#include "lightuinoMatrix.h"

unsigned char letters[][7] = { 
  
{  0xe,0x11,0x11,0x1f,0x11,0x11,0x11 },
{  0xf,0x11,0x11,0xf,0x11,0x11,0xf },
{  0xe,0x11,0x1,0x1,0x1,0x11,0xe },
{  0x7,0x9,0x11,0x11,0x11,0x9,0x7 },
{  0x1f,0x1,0x1,0xf,0x1,0x1,0x1f },
{  0x1f,0x1,0x1,0xf,0x1,0x1,0x1 },
{  0xe,0x11,0x1,0x1d,0x11,0x11,0xe },
{  0x11,0x11,0x11,0x1f,0x11,0x11,0x11 },
{  0xe,0x4,0x4,0x4,0x4,0x4,0xe },
{  0x10,0x10,0x10,0x10,0x11,0x11,0xe },
{  0x11,0x9,0x5,0x3,0x5,0x9,0x11 },
{  0x1,0x1,0x1,0x1,0x1,0x1,0x1f },
{  0x11,0x1b,0x15,0x11,0x11,0x11,0x11 },
{  0x11,0x11,0x13,0x15,0x19,0x11,0x11 },
{  0xe,0x11,0x11,0x11,0x11,0x11,0xe },
{  0xf,0x11,0x11,0xf,0x1,0x1,0x1 },
{  0xe,0x11,0x11,0x11,0x15,0x9,0x16 },
{  0xf,0x11,0x11,0xf,0x5,0x9,0x11 },
{  0x1e,0x1,0x1,0xe,0x10,0x10,0xf },
{  0x1f,0x4,0x4,0x4,0x4,0x4,0x4 },
{  0x11,0x11,0x11,0x11,0x11,0x11,0xe },
{  0x11,0x11,0x11,0x11,0x11,0xa,0x4 },
{  0x11,0x11,0x11,0x11,0x15,0x1b,0x11 },
{  0x11,0x11,0xa,0x4,0xa,0x11,0x11 },
{  0x11,0x11,0xa,0x4,0x4,0x4,0x4 },
{  0x1f,0x10,0x8,0x4,0x2,0x1,0x1f },
{  0xe,0x11,0x19,0x15,0x13,0x11,0xe },
{  0x8,0xc,0xa,0x8,0x8,0x8,0x8 },
{  0xf,0x10,0x10,0xe,0x1,0x1,0x1f },
{  0x1f,0x4,0x4,0xe,0x10,0x11,0xe },
{  0x8,0xc,0xa,0x1f,0x8,0x8,0x8 },
{  0x1f,0x1,0x1,0xe,0x10,0x10,0xf },
{  0xe,0x1,0x1,0xf,0x11,0x11,0xe },
{  0x1f,0x10,0x8,0x4,0x2,0x2,0x2 },
{  0xe,0x11,0x11,0xe,0x11,0x11,0xe },
{  0xe,0x11,0x11,0x1e,0x10,0x10,0xe },
{  0x1f,0x11,0x11,0x11,0x11,0x11,0x1f },
{  0x10,0x10,0x10,0x10,0x10,0x10,0x10 },
{  0x1f,0x10,0x10,0x1f,0x1,0x1,0x1f },
{  0x1f,0x10,0x10,0x1f,0x10,0x10,0x1f },
{  0x11,0x11,0x11,0x1f,0x10,0x10,0x10 },
{  0x1f,0x1,0x1,0x1f,0x10,0x10,0x1f },
{  0x1f,0x1,0x1,0x1f,0x11,0x11,0x1f },
{  0x1f,0x10,0x10,0x10,0x10,0x10,0x10 },
{  0x1f,0x11,0x11,0x1f,0x11,0x11,0x1f },
{  0x1f,0x11,0x11,0x1f,0x10,0x10,0x1f },
  
};


void LightuinoMatrix::print(int x, int y,const char* s,unsigned char spacing, GraphicOperation op)
  {
    //Serial.println("print");
    if (y>=Lightuino_NUMSRCDRVR) return; // Too far down
    
    while((*s)&&(x<Lightuino_NUMOUTS))
      {
        if ((*s >= 'A') && (*s <= 'Z') &&(x>=0))
          {
        int offset = (y*((Lightuino_NUMOUTS/8)+1)) + (x>>3);
        int shift  = x&7;
    //int split = (shift+5)-8;
        
        int charOff = offset;
        char whichLetter = (*s)-'A';

        //Serial.print(*s);
        //Serial.print(" ");
        //Serial.println((int)whichLetter);
        
        unsigned int mask;
        mask = 0b11111 << shift;
        //mask[1] = 0b11111 >> split;
        for (unsigned char i=0;(i<7)&&(y+i<Lightuino_NUMSRCDRVR);i++)
          {
          //Serial.println((int)(*((unsigned char*)(((unsigned char*)letters)+(whichLetter*7)+i))),BIN);
          //Serial.println((int)(letters[whichLetter][i]),BIN);
          if (op==DRAW)
            {
            *((unsigned int*)&videoRam[charOff]) &= ~mask;          
            *((unsigned int*)&videoRam[charOff]) |= (letters[whichLetter][i] << shift);
          //videoRam[charOff+1] |= letters[whichLetter][i] >> split;
            }
          else if (op==ERASE)
            {
            *((unsigned int*)&videoRam[charOff]) &= ~mask;              
            }
          else if (op==XOR)
            {
            *((unsigned int*)&videoRam[charOff]) ^= (letters[whichLetter][i] << shift);               
            }  
          else if (op==DRAW_BLACK_ON_WHITE)
            {
            *((unsigned int*)&videoRam[charOff]) &= ~mask;          
            *((unsigned int*)&videoRam[charOff]) |= ((~letters[whichLetter][i]&0b11111) << shift);

            }  
          charOff += ((Lightuino_NUMOUTS/8)+1);
            
          }
        //Serial.println("");
          }
        s++;
        x+=5 + spacing;
      }
  }
